﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
namespace DigDes.DSchool.SUPS.Web.UI
{
   
    public class CargoImage : IHttpHandler
    {
        private static readonly string ConnectionStringKey = "SUPSDB2";
        private static string connectionString = ConfigurationManager.ConnectionStrings[ConnectionStringKey].ConnectionString;
        
        public void ProcessRequest(HttpContext context)
        {
            string id = context.Request.QueryString["id"];

            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("SELECT Image FROM Cargo WHERE Cargo_ID = @ID", con);

            cmd.Parameters.Add("@ID", id);
            byte[] pict = null;
            con.Open();
            try
            {
                pict = (byte[])cmd.ExecuteScalar();
            }
            catch (Exception ex) { }
            con.Close();
            if (pict != null)
            {
                context.Response.ContentType = "image/jpeg";
                context.Response.OutputStream.Write(pict, 0, pict.Length);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}